package org.jtao.gbsubject.service;

import org.jtao.gbsubject.domain.TopSubject;
import org.jtao.gbsubject.repository.TopSubjectRepository;
import org.jtao.gbsubject.service.dto.TopSubjectDTO;
import org.jtao.gbsubject.service.mapper.TopSubjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * Service Implementation for managing TopSubject.
 */
@Service
@Transactional
public class TopSubjectService {

    private final Logger log = LoggerFactory.getLogger(TopSubjectService.class);

    private final TopSubjectRepository topSubjectRepository;

    private final TopSubjectMapper topSubjectMapper;

    public TopSubjectService(TopSubjectRepository topSubjectRepository, TopSubjectMapper topSubjectMapper) {
        this.topSubjectRepository = topSubjectRepository;
        this.topSubjectMapper = topSubjectMapper;
    }

    /**
     * Save a topSubject.
     *
     * @param topSubjectDTO the entity to save
     * @return the persisted entity
     */
    public TopSubjectDTO save(TopSubjectDTO topSubjectDTO) {
        log.debug("Request to save TopSubject : {}", topSubjectDTO);
        TopSubject topSubject = topSubjectMapper.toEntity(topSubjectDTO);
        topSubject = topSubjectRepository.save(topSubject);
        return topSubjectMapper.toDto(topSubject);
    }

    /**
     * Get all the topSubjects.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<TopSubjectDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TopSubjects");
        return topSubjectRepository.findAll(pageable)
            .map(topSubjectMapper::toDto);
    }

    /**
     * Get one topSubject by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public TopSubjectDTO findOne(Long id) {
        log.debug("Request to get TopSubject : {}", id);
        TopSubject topSubject = topSubjectRepository.findOne(id);
        return topSubjectMapper.toDto(topSubject);
    }

    /**
     * Delete the topSubject by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete TopSubject : {}", id);
        topSubjectRepository.delete(id);
    }

    public Integer importColleges(MultipartFile file){
        List<TopSubject> topSubjects=ImportService.importExcel(file,1,1,TopSubject.class);
        int sum=0;
        for(int i=0;i<topSubjects.size();i++){
            TopSubject topSubject = topSubjects.get(i);
            try{
                if(topSubject.getName()!=null){
                    TopSubject topSubject1=topSubjectRepository.findOneByName(topSubject.getName());
                    if(topSubject1==null){
                        sum++;
                        this.save(topSubjectMapper.toDto(topSubject));
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("导入数据一共【"+sum+"】行");
        return sum;
    }
}
